<?php

/**
* Google Checkout Component
* @author WalkerHamilton
* @license MIT
* @version 0.1
*/

/**
* Download the php docs from Google: http://code.google.com/apis/checkout/samplecode.html
* Drop CheckoutAPIFunctions.php, GlobalAPIFunctions.php, MerchantCalculationsAPIFunctions.php,
* OrderProcessingAPIFunctions.php, and ResponseHandlerAPIFunctions.php into a folder named "gcheckout"
* in your vendors folder
*
*	Make sure your component Class is working properly and the configuration options are set
*	Then put this at the top of any view in which you are referencing any method within this helper:
*
*	$googleCheckout->initialize($this);
*/

class GoogleCheckoutHelper extends Helper
{
	var $helpers = array('Session');
	
	var $_merchantID;
	var $_merchantKey;
	//1 == log to errorlog only, 2 == log to browser only, 3 == log to browser & error log
	var $_loggingLevel = 1;
	var $_currency;
	var $_testing;
	var $_approotdir = APP_DIR;
	var $_items = array();
	var $_gserve;
	var $_continueShopping;
	var $_helper;
	
	/*
	 * 
	 */
	function startup(&$helper)
	{
		//Is there a better way to include these depending on what's going on within the application?
		//For instance, if we're in shopping more, then we don't need request & response files
		//If we're in post-shopping mode, we don't need cart, item, shipping or tax (in all likelihood)
		vendor('gcheckout'.DS.'googlecart');
		vendor('gcheckout'.DS.'googleitem');
		vendor('gcheckout'.DS.'googleshipping');
		vendor('gcheckout'.DS.'googletax');
		vendor('gcheckout'.DS.'googlerequest');
		vendor('gcheckout'.DS.'googleresponse');

		/* We always need these */
		vendor('gcheckout'.DS.'googlemerchantcalculations');
		vendor('gcheckout'.DS.'googleresult');
		vendor('gcheckout'.DS.'googlelog');
	}

	function initialize($view_obj)
	{
		if(isset($view_obj->viewVars['checkout_config'])) {
			$this->_merchantID = $view_obj->viewVars['checkout_config']['id'];
			$this->_merchantKey = $view_obj->viewVars['checkout_config']['key'];
			$this->_currency = $view_obj->viewVars['checkout_config']['currency'];
			$this->_continueShopping = $view_obj->viewVars['checkout_config']['continue_shopping'];
			$this->_checkoutTesting = $view_obj->viewVars['checkout_config']['debug'];
	
			$this->_gserve = ($this->_checkoutTesting) ? 'sandbox' : 'Production';
		}
	}

	/*
	 * 
	 */
	function _restoreFromSessionArr() {
		if(empty($this->_items))
		{
			$sessR = $this->Session->read('Google');
			
			if($sessR==false) {
				return false;
			} else {
				$this->_items = unserialize($sessR);
				$this->_items = array($this->_items);
				$this->_items = $this->_items[0];
				return true;
			}
		}
	}

	/*
	 * This should be in the helper (and the helper should use the Session restore thinger)
	 */
	function makeButton($data=null, $theitems=null, $size='SMALL') {

		//pr($this);

		if($theitems) {
			
		} else if(!empty($this->_items)) {
			$theitems = $this->_items;
		} else if($this->_restoreFromSessionArr()) {
			$theitems = $this->_items;
		} else {
			trigger_error(__('(GoogleCheckoutHelper::makeButton) No items were specified either as the second argument of the makeButton method or were retrievable from the Session (perhaps set by the GoogleCheckout component?). If you’re working in php4, perhaps try calling $google_checkout->__destruct() manually in your action before allowing this view to render.', true), E_USER_WARNING);
			exit();
		}
		
		if($size!='SMALL' && $size!='MEDIUM' && $size!='LARGE') {
			$size = 'SMALL';
		}
		
		$cart = new GoogleCart($this->_merchantID, $this->_merchantKey, $this->_gserve, $this->_currency);
		
		//pr($cart);
		
		$counter = 1;

		foreach($theitems as $item)
		{
			$varvar = 'item_'.$counter;
			${$varvar} = new GoogleItem($item['title'], // Item name
									$item['desc'], // Item description
									$item['count'], // Quantity
									$item['price'] // Unit price
									);
			if(isset($item['data']))
			{
				${$varvar}->SetMerchantPrivateItemData(new MerchantPrivateItemData($item['data']));
			}
			if(isset($item['id'])) { ${$varvar}->SetMerchantItemId($item['id']); }
			$cart->AddItem(${$varvar});
		}
		// Set the acct. name, etc.
		if($data)
		$cart->SetMerchantPrivateData(new MerchantPrivateData($data));
		
		// Specify "Return to xyz" link
		$cart->SetContinueShoppingUrl($this->_continueShopping);
		
		// Display Google Checkout button
		return $cart->CheckoutButtonCode($size);
	}

}

?>